package com.qiqi.mealordersystem.Service;

import com.alibaba.fastjson.JSON;
import com.qiqi.mealordersystem.Mapper.UserMapper;
import com.qiqi.mealordersystem.model.Result.ResultInfo;
import com.qiqi.mealordersystem.model.User;
import com.qiqi.mealordersystem.model.vo.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    private UserMapper mapper;
    private ResultInfo resultInfo;

    @Autowired
    public UserService(UserMapper mapper) {
        this.mapper = mapper;
    }

    public String regiseter(String userString){
        User user = JSON.parseObject(userString, User.class);
        Integer result = mapper.insertUser(user);
        if(result > 0){
            resultInfo = new ResultInfo(result,1,"注册成功");
        }
        else {
            resultInfo = new ResultInfo(0,"注册失败");
        }
        return JSON.toJSONString(resultInfo);

    }

    public String getUserDetail(String userId){
        User user1 = new User();
        int i = Integer.parseInt(userId);
        user1.setUserid(i);
        UserDetail userDetail = new UserDetail();
        User user = getUser(user1);
        if(user != null){
            userDetail.setUserid(user.getUserid());
            userDetail.setUsername(user.getUsername());
            userDetail.setSex(user.getSex());
            userDetail.setEmail(user.getEmail());
            userDetail.setPhone(user.getPhone());
            userDetail.setBirthday(user.getBirthday());
        }
        return JSON.toJSONString(userDetail);
    }

    public User getUser(User user){

        User user1 = mapper.selectUser(user);
        return user1;

    }

}
